#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include "header.h"

int menu()
{
SDL_Surface* affichage;
SDL_Event event;
SDL_Surface *ecran1, *image1;


   // SDL_Rect positionsouris;
    int done=1, continuer = 1, *amp;

    SDL_Surface *ecran=NULL, *image;
    SDL_Surface *score;
    SDL_Surface *regle;
    SDL_Surface *jouer;
    SDL_Surface *option;
    SDL_Surface *son;

    SDL_Rect  desti;
    SDL_Rect desti1;
    SDL_Rect desti2;
    SDL_Rect desti3;
    SDL_Rect desti4;



    atexit(SDL_Quit);
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf( "Echec d'initialisation de SDL");
        return 1;
    }
    printf("SDL est initialisé avec succès.\n");
    ecran = SDL_SetVideoMode(960, 640, 32, SDL_ANYFORMAT|SDL_FULLSCREEN);

    if ( ecran == NULL )
    {
        printf("Echec de creation de la fenetre  : %s.\n",SDL_GetError());
        return 1;
    }


    image = SDL_LoadBMP("media/img/fort.bmp");
    if ( image == NULL )
    {
        printf( "Echec de chargement du fichier fort.bmp");
        return 1;
    }
    score = SDL_LoadBMP("media/img/bouton.bmp");
    if (score == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    regle= SDL_LoadBMP("media/img/bouton.bmp");
    if (regle== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    jouer= SDL_LoadBMP("media/img/bouton.bmp");
    if (jouer== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    option = SDL_LoadBMP("media/img/bouton.bmp");
    if (option == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    son = SDL_LoadBMP("media/img/bouton.bmp");
    if ( son == NULL )
    {
        printf( "Echec de chargement du fichier image.bmp : %s.\n", SDL_GetError());
        return 1;
    }
    SDL_SetColorKey(score, SDL_SRCCOLORKEY, SDL_MapRGB(score->format, 255, 255, 255));
    SDL_SetColorKey(regle, SDL_SRCCOLORKEY, SDL_MapRGB(regle->format, 255, 255, 255));
    SDL_SetColorKey(jouer, SDL_SRCCOLORKEY, SDL_MapRGB(jouer->format, 255, 255, 255));
    SDL_SetColorKey(option, SDL_SRCCOLORKEY, SDL_MapRGB(option->format, 255, 255, 255));
    SDL_SetColorKey(son, SDL_SRCCOLORKEY, SDL_MapRGB(son->format, 255, 255, 255));


//Construct the destination rectangle for our blit
    desti.x = 70;
    desti.y = 420;
    desti.w = 190;
    desti.h = 70;
    
    desti1.x = 70;
    desti1.y = 300;
    desti1.w =190;
    desti1.h = 70;
    
    desti2.x = 70;
    desti2.y = 170;
    desti2.w = 190;
    desti2.h = 70;
    
    desti3.x = 790;
    desti3.y = 540;
    desti3.w = option->w;
    desti3.h = option->h;
    
    desti4.x = 785;
    desti4.y = 50;
    desti4.w = son->w;
    desti4.h = son->h;
    
    SDL_BlitSurface(image, NULL, ecran,NULL);
    SDL_BlitSurface(score, NULL, ecran, &desti);
    SDL_BlitSurface(regle, NULL, ecran,&desti1 );
    SDL_BlitSurface(jouer, NULL, ecran, &desti2);
    SDL_BlitSurface(option, NULL, ecran, &desti3);
    SDL_BlitSurface(son, NULL, ecran, &desti4);


    SDL_UpdateRect(ecran, 0, 0, 0, 0);
    SDL_EnableKeyRepeat(10,10);




    while(done)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = 0;
                break;

            case SDL_MOUSEBUTTONUP:


                if (event.button.button == SDL_BUTTON_LEFT && desti2.x < event.button.x && event.button.x < desti2.x + jouer->w &&
                        desti2.y < event.button.y && event.button.y < desti2.y + jouer->h)
                    play();


                else if (event.button.button == SDL_BUTTON_LEFT && desti1.x < event.button.x && event.button.x < desti1.x  + regle->w &&
                         desti1.y < event.button.y && event.button.y < desti1.y + regle->h)
                    reglejeu();
                break;
            case SDL_KEYDOWN:
            {

                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = 0;
                break;
            }

           case SDL_MOUSEMOTION:
                       {

                if  ( desti1.x < event.motion.x && event.motion.x < desti1.x + 190 &&
                        desti1.y < event.motion.y && event.motion.y < desti1.y + 70)

                {
                    SDL_BlitSurface(regle, NULL, ecran, NULL);
                    SDL_UpdateRect(ecran, 0, 0, 0, 0);

                }

                else if ( desti2.x < event.motion.x && event.motion.x < desti2.x + 190 &&
                          desti2.y < event.motion.y && event.motion.y < desti2.y + 70)

                {
                    SDL_BlitSurface(jouer, NULL, ecran, NULL);
                    SDL_UpdateRect(ecran, 0, 0, 0, 0);

                }


                else if ( desti.x < event.motion.x && event.motion.x < desti.x + 190 &&
                          desti.y < event.motion.y && event.motion.y < desti.y + 70)

                {
                    SDL_BlitSurface(score, NULL, ecran, NULL);
                    SDL_UpdateRect(ecran, 0, 0, 0, 0);

                }

                else
                {
                    SDL_BlitSurface(image, NULL, ecran, NULL);
                    SDL_UpdateRect(ecran, 0, 0, 0, 0);

                }
            }

              
                break;

            }
        }
    }

    SDL_Quit();

    return EXIT_SUCCESS;
    }
